package com.xiaoyu.lanling.feature.mine.controller

import android.view.View
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.user.UserMineEvent
import com.xiaoyu.lanling.event.user.VisitorReadEvent
import com.xiaoyu.lanling.feature.user.data.UserInfoData
import com.xiaoyu.lanling.feature.user.model.UserExtra
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.util.TextViewUtil
import kotlinx.android.synthetic.main.fragment_main_mine.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainMineViewController(val fragment: BaseFragment) {

    private val requestTag = Any()

    init {
        initView()
        initBind()
        initEvent()
        initData()
    }

    private fun initData() {
        UserInfoData.mine(requestTag)
    }

    fun onStart(isFirstTimeStart: Boolean) {
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(fragment, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: UserMineEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                UserExtra.instance.updateVipType(event.vipInfo.type)
                showData(event)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: VisitorReadEvent) {
                fragment.visitor_count.visibility = View.INVISIBLE
            }
        })
    }

    private fun showData(event: UserMineEvent) {
        fragment.tag_text_view.text = event.tag
        fragment.sex_and_age.text = event.age.toString()
        fragment.visitor_layout.setViewTag(event.visitorCount)
        TextViewUtil.displayFor999PlusType(fragment.visitor_count, event.visitorCount)
        ImageViewUtil.loadImage(fragment.vip_icon, event.vipInfo.imageLoadParam)
        TextViewUtil.displayOrGone(fragment.charge_tag, event.chargeTag)
    }

    private fun initView() {
        val user = UserData.getInstance().user
        fragment.id_desc.text = fragment.getString(R.string.main_mine_user_info_id, user.uid)
        fragment.name.setUser(user)
        ImageViewUtil.loadImage(fragment.avatar, user, 72, 1, R.color.user_info_avatar_border)
        fragment.sex_and_age.setCompoundDrawablesRelativeWithIntrinsicBounds(if (user.isMale) R.drawable.user_sex_male_icon else R.drawable.user_sex_female_icon, 0, 0, 0)
        fragment.sex_and_age.background = AppContext.getDrawable(if (user.isMale) R.drawable.user_sex_male_background_shape else R.drawable.user_sex_female_background_shape)
    }

    private fun initBind() {
        fragment.edit_tip.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            Router.instance.gotoUserActivity(activity, UserData.getInstance().user)
        }
        fragment.invite_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            Router.instance.gotoInviteActivity(activity)
        }
        fragment.charge_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            Router.instance.gotoCoinProductListAndBalanceActivity(activity)
        }
        fragment.income_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            Router.instance.gotoCoinIncomeActivity(activity)
        }
        fragment.task_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            Router.instance.gotoMissionListActivity(activity)
        }
        fragment.guard_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            Router.instance.gotoGuardListActivity(activity)
        }
        fragment.setting_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            Router.instance.gotoSettingActivity(activity)
        }
        fragment.vip_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            if (UserExtra.instance.isVip) {
                Router.instance.gotoVipMineActivity(activity)
            } else {
                Router.instance.gotoVipProductListActivity(activity, Constant.From.VIP_CENTER)
            }
        }
        fragment.visitor_layout.setOnClickDebounceListener {
            val activity = fragment.activity ?: return@setOnClickDebounceListener
            val visitorCount = it.getViewTag<Int>() ?: return@setOnClickDebounceListener
            Router.instance.gotoVisitHistoryActivity(activity, visitorCount)
        }
    }
}